import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { config } from '../../shared/smartadmin.config';

declare var $: any;
@Component({
	selector: 'sa-factor-queue',
	templateUrl: './factor-queue.component.html',
})
export class FactorQueueComponent implements OnInit {
	tableData:any[] = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	searchKey:string = '';
	isSendCoef = ""
	deviceId:string = '';
	carNumber:string = '';
	dateType:string = 'startTime';
	sensorStatus:string|number = '';		//传感器状态，0无损坏，1有损坏
	selectedCompanyId:string = '';
	selectedCompanyName:string = '';
	startTime:string = '';//table时间控件
	endTime:string = '';//table时间控件
	constructor(private jsonApiService: JsonApiService, private router: Router, private activatedRoute: ActivatedRoute ) {
		activatedRoute.queryParams.subscribe(queryParams => {
			this.curPage = queryParams.nowPage||1;
		});
	}
	//时间change回调
	getSelectTableTime(val){
		this.startTime = val.startTime;
		this.endTime = val.endTime;
	} 
	ngOnInit(){
		this.load();
	}
	//table刷新
    refresh(){
    	this.load();
    }
	//表格搜索
	table_search(){
		this.curPage = 1;
		this.load();
	}
	//初始化加载list数据
	load(){
		let timeCheck = common.correctTimeParams(this.startTime,this.endTime,0);
		if(!timeCheck.status){
			this.jsonApiService.message(timeCheck.message);
			return;
		}
		let data = {
			pageSize:this.pageSize,
			nowPage:this.curPage,
			parameters:{
				companyId:this.selectedCompanyId,
				deviceId:this.deviceId,
				startDate:this.startTime,
				endDate:this.endTime,
				isSendCoef: this.isSendCoef,
				searchKey:common.stringTrim(this.searchKey)
			}
		}
		let reqdata = 'gridPager='+JSON.stringify(data);
		this.jsonApiService.http("deviceCoefHandler/getCoefHandlerForPage.html", reqdata).subscribe(data => {
			if(data.isSuccess){
				this.tableData = data.exhibitDatas;
				this.totalCount = data.recordCount;
				for(let item of data.exhibitDatas){
					if(item.newCoef){
						let coefArr = [];
						for (let i = 0;i < item.newCoef.length; i++){
							let _item = item.newCoef[i];
							if(_item){
								let obj:any = {
									value:_item,
									index:i+1
								}
								coefArr.push(obj);
							}
						}
						item.newCoefT = coefArr;
					}else{
						item.newCoefT = [];
					}
					if(item.oldCoef){
						let oldCoef = [];
						for (let i = 0;i < item.oldCoef.length; i++){
							let _item = item.oldCoef[i];
							if(_item){
								let obj:any = {
									value:_item,
									index:i+1
								}
								oldCoef.push(obj);
							}
						}
						item.oldCoefT = oldCoef;
					}else{
						item.oldCoefT = [];
					}
				}
			}else{
				this.jsonApiService.message(data.message);
			}
		})
	}
	//系数下发
	factorSetting(row){
		this.jsonApiService.confirm_message("确认要下发?").then(()=>{
			this.jsonApiService.http("deviceCoefHandler/handlerCoefById.html",'id='+row.id).subscribe(data=>{
				this.jsonApiService.message(data.message);
				if(data.success){
					this.load();
				}
			})
		})
	}
	//删除
	delRow(row){
		this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(()=>{
			this.jsonApiService.http("deviceCoefHandler/deleteById.html",'id='+row.id).subscribe(data=>{
				this.jsonApiService.message(data.message);
				if(data.success){
					this.load();
				}
			})
		})
	}
	//清空数据
	table_clear(){
		
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//公司选中回调
	companySelected(data){
		this.selectedCompanyId = data.companyId;
		this.selectedCompanyName = data.companyName;
		this.deviceId = data.deviceId;
		this.carNumber = data.carNumber;
	}
}